homework for cs python, Project Objectives Introduce you to the implementation of simulation codes following a Top-Down design. Reinforce your knowledge in importing...
homework for cs python,
Project Objectives
- Introduce you to the implementation of simulation codes following a Top-Down design.
- Reinforce your knowledge in importing different libraries (pre-installed or from a provided python file) and use functions defined in them.
- Practice decision and loop structures, functions, lists, and the use of the library
matplotlib. - Learn how to use Unit Tests to verify the correctness of your project subparts.
What is a Unit Test?
Unit Testing is a practice of independently testing parts of your programs such as functions. It gives programmers the ability to verify that functions work as expected. That is to say, for any given function and a given set of inputs, a Unit Test validates whether or not the function is returning the proper values.
In this project, you will be given Unit Tests to help you validate the functionality of your program as you progress. The test functions' code are provided to you in the file testCases.py. This file contains the definitions of all the function that test each of the functions you need to implement. Details about how to use this file are provided in a later section.
Be aware that you can test your functions using the method followed in Project 2. Namely, you can import your code (project3.py) to IDLE and use the provided test case codes to test from it.
Please notice that the provided skeleton include the definition of the functions with a dumb print in each. This print must be removed before you start coding. The reason of providing you a dumb print statement in each function is to avoid errors when you import your code (project3.py) to either IDLE ortestCases.py. Python throw an error when a function that is defined without any instruction in it is imported.
Setting up the environment
Go to your working directory in “data.cs.purdue.edu” and create the directory “cs177/project3”. Refer to the lab01 if you need to remember the steps to do so. Make sure that you import the following libraries:matplotlib, sorter, tabulate. The last two are provided to you in the package below.
Your project package is here. Download this package, and start working on the file called project3.py
This package contain the following files:
- groupInfo.txt: File with information about the groups of teams of the world cup.
- round0Results.txt: File containing the results of round 0 stage matches. This is required to determine the winners in round 0.
- breaker.txt: File required to determine the winners in round 1, 2 and 3.
- sorter.py: Library containing the function that sorts tables.
- tabulate.py: Library used in main to achieve a better look of the printed tables.
- project3.py: Skeleton of the project. Start working here!!!
- testCases.py: File that allows you to test each of the 9 functions you must implement in this project. Also the file includes test cases to test the function
sortTablein sorter.py. You are not required to implement the functionsortTablein this project, you just need to use it. The cases for this function are intended to illustrate the functionality of the function so that you can become familiar with it.
PROJECT DESCRIPTION
This cup will be played by 16 countries grouped into four groups: A, B C and D as it is depicted above.
Tournament Rules:
There will be 4 rounds of elimination starting from round 0. Round 4 is the final round of the tournament. In each round a number of games (hereafter, the terms 'game' and 'match' are used interchangeably) are played and the results of those games determine the matches to be played in the next round. The selection process for the next rounds is explained as follows. Please refer to the image below to have a better understanding of the tournament rules.
- Round 0: In round 0 every team will play one game with each of the other members of its group. A team accumulate points depending on its number of wins, draws and looses. Only the best two teams of the group qualify for the round 1. Between the two best teams of a group there will always be a winner (the best team of the group) and a runner-up (the second best team of the group). In round 0 the best two teams of a group are crossed with the best two teams of another group. Specifically, the winner of group A is matched with the runner-up of group B, and the winner of group B is matched with the runner-up of group A to play in round 1. Similarly, the winner of group C is matched with the runner-up of group D, and the winner of group D is matched with the runner-up of group C to play in round 1.
- Round 1: In round 1 there will only be 4 games: (1) Winner of group A vs Runner-up of group B, (2) Winner of group B vs Runner-up of group A, (3) Winner of group C vs Runner-up of group D, and (4) Winner of group D vs Runner-up of group C. Winners of the games qualify for round 2 and thus forwarded. Losers are eliminated. In this occasion groups are not crossed as happened in round 0. The winner of game (1) is matched with the winner of game (2) to play in round 2, and thus the winner of the groups A and B is determined. Likewise, the winner of game (3) is matched with the winner of game (4) to play in next round and the winner of the groups C and D is determined.
- Round 2: This round is the semifinal of this world cup tournament. In this round, there will be only 2 games. One is to determine the winner of the groups A and B, and another is to determine the winner of the groups C and D. The winner of groups A and B qualifies for (i.e., passes to) the final. In a similar manner, the winner of groups C and D make its way to the final. The losers of the games in round 2 fight for the third place deciding game in the next round.
- Round 3: This is the final of the tournament. In this stage, there will be 2 games. One for the championship and another for the third place. Since it is the last round no more matching need to be done for the next round.
Statistics: Obtaining the final results of the tournament, you are required to plot some statistics of the results of round 0 about the champion, sub-champion (i.e., the runner-up) and third-place winning teams to see whether the performance of these teams in round 0 reflects the results of the final.
Point Rules:
As it was mentioned, in round 0 every team plays one game with each of the other members of its group. In every game each team accumulate points according to the following rules:
- Wins: 3 pts
- Draws: 1 pts
- Losses: 0 pts
Expected output at the completion of the project:
In this section we provide you with a sample output of the project after completion. The idea is to give you a clear understanding of your goals here. We will walk through the tasks you need to complete in each round and present the results you are expected to have at the end of each of them. Also, we go over the tasks you need to complete after finishing all the rounds. The results presented here and throughout this document were obtained with the files groupInfo.txt, round0Results.txt, and breaker.txt provided to you.
Round 0: The results of the matches of round 0 will be provided to you in a randomly generated file called round0Results.txt. The information about the groups A, B, C, and D will be also provided to you in the file groupInfo.txt. In round 0 you are required to parse these files and create a rank table for each of the groups. The rank tables will allow you to pick the best two teams of each group and then cross them to get the two crossed tables to be used in round 1. Let us see an example of the expected output after concluding round 0:
>>> ================================ RESTART ================================ >>> *************************************************************** * Rank Tables after ROUND 0 * *************************************************************** Rank table for groupA Team Point Rank -------- ------- ------ France 7 1 Japan 4 2 Colombia 3 3 Mexico 3 4 Rank table for groupB Team Point Rank ------------ ------- ------ Argentina 9 1 India 6 2 Saudi Arabia 3 3 USA 0 4 Rank table for groupC Team Point Rank ----------- ------- ------ Germany 7 1 Netherlands 6 2 Panama 3 3 Bangladesh 1 4 Rank table for groupD Team Point Rank ------- ------- ------ England 9 1 Italy 6 2 Brazil 1 3 Spain 1 4 Matches for next round resulting of crossing group A and B Team1 Team2 --------- ------- France India Argentina Japan Matches for next round resulting of crossing group C and D Team1 Team2 ------- ----------- Germany Italy England Netherlands
In round 0 your task is to complete the functions that create the rank tables and the functions that create the crossed tables. The rank table has to be created in such a way that all the teams in a group are assigned an unique ranking. Mechanisms to break the ties are also required to be implemented and they are part of your tasks in this project. You will see more details about this in the section YOUR TASKSpresented later in this document.
Round 1: For this round you need to determine the winner of the crossed tables. You are expected to write a function able to determine the winner between two teams. This function will use the filebreaker.txt. This file contains a match between each two of the 16 teams. The results of these matches are guaranteed to determine a winner and a loser. Note that the breaker.txt file does not contain a match's result with draw. Once the winners of round 1 are determined, the match tables for round 2 are created. Let us see an example of the expected output after concluding round 1:
*************************************************************** * ROUND 1 * *************************************************************** Results of the matches of the crossed groups A and B Team1 Team2 Winner --------- ------- --------- France India India Argentina Japan Argentina Results of the matches of the crossed groups C and D Team1 Team2 Winner ------- ----------- -------- Germany Italy Germany England Netherlands England Match tables of groups A and B for semi-finals Team1 Team2 ------- --------- India Argentina Match tables of groups C and D for semi-finals Team1 Team2 ------- ------- Germany England
Round 2: Here, we need to determine the winner of the match tables created in the previous round. You will reuse the same function written in the previous round. Again, this function works with the filebreaker.txt to determine which teams win the matches. An example of the expected output after round 2 is as follows:
*************************************************************** * SEMI FINALS * *************************************************************** Results of the matches of groups A and B in semi-finals Team1 Team2 Winner ------- --------- -------- India Argentina India Results of the matches of groups C and D in semi-finals Team1 Team2 Winner ------- ------- -------- Germany England England Match table for championship Team1 Team2 ------- ------- India England Match table for third place Team1 Team2 --------- ------- Argentina Germany
Round 3: Here we determine the winner of the match tables created in the previous round to determine the champion, sub-champion (i.e., the runner-up) and third-place winning teams of the world cup. How? Reusing previously created functions and the breaker.txt file. An example of the output after the final round:
*************************************************************** * FINALS * *************************************************************** Results of the championship match Team1 Team2 Winner ------- ------- -------- India England England esults of the third place match Team1 Team2 Winner --------- ------- --------- Argentina Germany Argentina Finals results: Champion Sub-Champion Third Place ---------- -------------- ------------- England India Argentina
Statistics: You are required to plot a bar chart of the number of goals for and against in round 0 for the champion, sub-champion and third-place winning teams. For this your function must use the fileround0Results.txt. Using the provided files you are expected to plot a bar chart that looks like below:
*************************************************************** * Statistics * *************************************************************** Do the results of round 0 coincide with the forecast? Let's compare the scores of the champion, sub-champion and third-place
IMPLEMENTATION DETAILS
Top-Down Design:
In this section we will provide you with some hints and details about how to implement your solution. We will do so walking through the tasks you need to complete in each round and the required tasks after the completion of all the rounds. Before continuing we urge you to carefully study the Top-Down Design presented below.
The functions main and sortTable will be provided to you. YOU DO NOT HAVE TO CHANGE THEM. You will be in charge of writing the remaining 9 functions. We recommend you to visit this link to learn know to use sortTable. The link includes the output of the test cases provided in testCases.py for this function.
Be aware that some functions depend on others. We call these functions dependent functions. Also, there are functions that do not depend on any other function. This functions are independent functions. We highly recommend you to write independent functions first since dependent functions cannot be tested without the completion of the functions they depend on.
As an example, the function buildRankTable cannot be tested without completing first the functionsreadGroupInfo, readResultsRound0, buildPointsTable, and addRankToPointsTable. In this casebuildRankTable is a dependent function and readGroupInfo, for example, is an independent function. Notice that addRankToPointsTable is a dependent function that depends on the implementation of breakTies.
List of functions (tasks) to be completed:
Below we list the functions you need to complete in the project divided by rounds. Because of the dependency of some functions on others we recommend you to implement your functions in the order provided below. Notice that this order corresponds to the order of the 9 tasks listed in the next section and the order of functions in the file project3.py.
readGroupInfo(round0)readResultsRound0(round0)buildPointsTable(round0)breakTies(round0)addRankToPointsTable(round0)buildRankTable(round0)buildCrossedTable(round0)determineWinner(round 1, 2, and 3)plotGoalsBarChar(After completion of rounds)
As you can see round 0 requires the highest load of works since 7 functions must been competed in it. Rounds 1, 2 and 3 requires only one function. After the winners of the world cup (Champion, sub-Champion, and Third-Place) are determined, a bar chart is plotted.
How to test your functions:
In order to test your code you will use the file testClasses.py. You only need to uncomment the task you want to test one at a time in the function main. The content of this file is as follows:
##########################################################
# Required Imports #
##########################################################
from project3 import buildRankTable
from project3 import readGroupInfo
from project3 import readResultsRound0
from project3 import buildPointsTable
from project3 import addRankToPointsTable
from project3 import breakTies
from project3 import buildCrossedTable
from project3 import determineWinner
from project3 import plotGoalsBarChar
from sorter import sortTable
##########################################################
# Test cases for Task 1 - Task 9 #
##########################################################
def testTask1():
teams = readGroupInfo('groupA')
for t in teams:
print (t)
def testTask2():
teams = ['Colombia', 'Japan', 'Mexico', 'France']
results = readResultsRound0(teams)
for r in results:
print (r)
def testTask3():
teams = ['Colombia', 'Japan', 'Mexico', 'France']
results = ['Mexico, France, 4, 6', \
'Japan, Mexico, 8, 2', \
'Colombia, Mexico, 3, 9', \
'Colombia, Japan, 9, 7', \
'Colombia, France, 2, 8', \
'Japan, France, 4, 4']
pointsTable = buildPointsTable(teams, results)
for p in pointsTable:
print (p)
def testTask4():
rankTable = [['France', 7, 1], \
['Japan', 4, 2], \
['Colombia', 3, 3], \
['Mexico', 3, 4]]
rankTable = breakTies(rankTable)
for r in rankTable:
print (r)
def testTask5():
pointsTable = [['Colombia', 3], \
['Japan', 4], \
['Mexico', 3], \
['France', 7]]
rankTable = addRankToPointsTable(pointsTable)
for r in rankTable:
print (r)
def testTask6():
group = 'groupA'
rankTableA = buildRankTable(group)
print(rankTableA)
def testTask7():
rankTableA = [['Japan', 6, 1], ['Colombia', 4, 2],\
['Mexico', 4, 3], ['France', 3, 4]]
rankTableB = [['USA', 9, 1], ['Saudi Arabia', 4, 2], \
['India', 3, 3], ['Argentina', 1, 4]]
crossedTableAB = buildCrossedTable(rankTableA, rankTableB)
print(crossedTableAB)
def testTask8():
teams = ['Panama', 'India']
winner = determineWinner(teams)
print(winner)
def testTask9():
finalResults = ['Brazil', 'Germany', 'Italy']
plotGoalsBarChar(finalResults)
##########################################################
# Test cases for Sort Table (Optional) #
##########################################################
def test1SortTable():
rankTable = [['Brazil', 6, 1], \
['Bangladesh', 6, 1], \
['Panama', 6, 1], \
['Argentina', 0, 4]]
rankTable = sortTable(rankTable, 0, 0, 2, 'ascending')
for r in rankTable:
print (r)
def test2SortTable():
pointsTable = [['Bangladesh', 1], \
['Brazil', 4], \
['Panama', 6], \
['Argentina', 4]]
pointsTable = sortTable(pointsTable, 1, 0, len(pointsTable)-1, 'descending')
for p in pointsTable:
print (p)
##########################################################
# The main function contains the call to all the tests #
# Uncomment the desired test one at a time #
##########################################################
def main():
# Test for tasks
#testTask1()
#testTask2()
#testTask3()
#testTask4()
#testTask5()
#testTask6()
#testTask7()
#testTask8()
#testTask9()
# Test for sort table. Just for you to get familiar with the function
#test1SortTable()
#test2SortTable()
main()As you can see the function main of this file contains a call to the test case of each function. You just need to uncomment in main for the desired test one at a time. In the next section, YOUR TASKS, we will present per task the piece of code of testCases.py that test each function and its corresponding result. Feel free to test your code from IDLE using this information.
Be aware that the definition of functions provided to you in project3.py includes a print statement you need to remove before start coding your solution. The purpose of that print statement is preventing errors when you test the functions independently. Since testCases.py imports each of the functions in project3.py, functions inproject3.py need to be complete with at least one instuction to avoid errors.
YOUR TASKS:
Task 0: Set your environment using the content of the project 3 package
This task gives you instructions about how to set up your environment using some of the files provided to you in project 3 package, and how to submit your final file.
Before you start coding your solution in project3.py you need to procceed as follows:
- Import
matplotlib - Import function
tabulateprovided to you in tabulate.py - Import function
sortTableprovided to you in sorter.py - Make sure you DO NOT call the function
maina the end of the file project3.py while you are implementing your solution (that is, the instructionmain()must not be present in your file). You must implement your functions without having that instruction at the end of the file to avoid errors when you use testCases.py to test your functions.
After you finish coding your functions:
- Call the function
main(that is, write instructionmain()) at the end of the file project3.py ONLY when you have completed all the functions. Run your solution. The expected output must be the one showed in the section Project Description above. - After testing your solution REMOVE the call to
mainat the end of the file project3.py and submit your solution without that instruction.
Task 1: Write the function that reads information of the groups from the file groupInfo.txt
In this task you will implement the function readGroupInfo(groupName).
This function reads groupInfo.txt file and finds the teams that belong to groupName given as parameter to the function. groupName is of string datatype. The four possible values of groupName are:'groupA', 'groupB', 'groupC', 'groupD'. You must not use other values for goupName.
readGroupInfo function returns a list of team names that belong to the given groupName. E.g., teams = [ 'Colombia', 'Japan', 'Mexico', 'France'] if the value of groupName is 'groupA'.
Unit Test from file testCases.py: testTask1() is a unit test for function readGroupInfo.
def testTask1():
teams = readGroupInfo('groupA')
for t in teams:
print (t)Sample Output: Expected output of testTask1() is following:
>>> ================================ RESTART ================================ >>> Colombia Japan Mexico France >>>
Task 2: Write the function that reads the results of round 0 from the file round0Results.txt
In this task you will implement the function readResultsRound0(teams).
The function 'readResultsRound0' receives teams, a list of strings as parameters. E.g., teams = ['Brazil', 'Netherlands', 'Panama', 'Italy']. This function reads round0results.txt file and finds the matches of the teams given in the list teams.
It returns a list of strings containing the results of each team that belongs to list teams. The returned list should not contain any duplicate items. For example, the returned results is like following:
results = ["Brazil, Netherlands, 4, 1", \
"Panama, Netherlanders, 0, 4", \
"Italy, Brazil, 7, 6", \
"Italy, Panama, 2, 5", \
"Brazil, Panama, 4, 3", \
"Italy, Netherlands, 8, 7"]In general, The format of the return list is as follows:
["<t1>,<t2>,<s1>,<s2>", \ "<t1>,<t3>,<s1>,<s2>", \ "<t1>,<t4>,<s1>,<s2>", \ "<t2>,<t3>,<s1>,<s2>", \ "<t2>,<t4>,<s1>,<s2>", \ "<t3>,<t4>,<s1>,<s2>"]
The format of each string is: “<ti>,<tj>,<si>,<sj>”, where:
- <ti>: Team i of the group
- <tj>: Team j of the group
- <si>: Score of Team i in the match
- <sj>: Score of Team j in the match
Unit Test from file testCases.py: testTask2() is a unit test for function readResultRound0.
def testTask2():
teams = ['Colombia', 'Japan', 'Mexico', 'France']
results = readResultsRound0(teams)
for r in results:
print (r)Sample Output: Expected output of testTask2() is following:
>>> ================================ RESTART ================================ >>> Mexico, France, 4, 6 Japan, Mexico, 8, 2 Colombia, Mexico, 3, 9 Colombia, Japan, 9, 7 Colombia, France, 2, 8 Japan, France, 4, 4 >>>
Task 3: Write the function that builds the Points Table
In this task you will implement the function buildPointsTable(teams, results).
The function buildPointsTable(teams, results) creates a table of points for each team in a specific group. It receives two lists:
teams: list of strings. E.g., ['England', 'Spain', 'Germany', 'Bangladesh']results: list of strings, where each string represents a match result between two teams of the same group. The format of the list is as follows:
["<t1>,<t2>,<s1>,<s2>", \ "<t1>,<t3>,<s1>,<s2>", \ "<t1>,<t4>,<s1>,<s2>", \ "<t2>,<t3>,<s1>,<s2>", \ "<t2>,<t4>,<s1>,<s2>", \ "<t3>,<t4>,<s1>,<s2>"]
The format of each string is: “<ti>,<tj>,<si>,<sj>”, where:
- <ti>: Team i of the group
- <tj>: Team j of the group
- <si>: Score of Team i in the match
- <sj>: Score of Team j in the match
Notice that each groups contains 4 teams. The function produces a table of the following format:
| <Team> | <Points> |
This table is represented as a list of lists. Every inner list contains the pair listed above, where <Team> is a string data type, and <Points> an integer data type. For example:
pointsTable:
[ ['England', 5], ['Spain', 2], ['Germany', 5], ['Bangladesh', 4] ]
Datatypes of the pointsTable is following: [ [str, int], [str, int]… [str, int] ].
Please make sure the order of the teams in the pointsTable will be the same as the order of the team name in the teams list.
The points of each group is calculated by adding the points obtained in each following the rules :
- Win (3 pts)
- Draw (1 pts)
- Loss (0 pts)
Finally, buildPointsTable function returns the pointsTable.
Unit Test from file testCases.py: testTask3() is a unit test for function buildPointsTable
def testTask3():
teams = ['Colombia', 'Japan', 'Mexico', 'France']
results = [ 'Mexico, France, 4, 6', \
'Japan, Mexico, 8, 2', \
'Colombia, Mexico, 3, 9', \
'Colombia, Japan, 9, 7', \
'Colombia, France, 2, 8', \
'Japan, France, 4, 4' ]
pointsTable = buildPointsTable(teams, results)
for p in pointsTable:
print(p) Sample Output: Expected output of testTask3() is following:
>>> ================================ RESTART ================================ >>> ['Colombia', 3] ['Japan', 4] ['Mexico', 3] ['France', 7] >>>
Task 4: Write the function that breaks ties in ranking values of the Rank Table
In this task, you need to write the function breakTies(rankTable). We recommend you to understandaddRankToPointsTable in task 5 since breakTies(rankTable) is called from that function.
The function breakTies(rankTable) breaks the ties among teams with equal ranks within a group. It receives a rankTable that may include teams with equal rank, i.e., a tie.
The format of the received rankTable is as follows (see Task 5 for better understanding):
| <Team> | <Point> | <Rank> |
This table is represented as a list of lists. Every inner list contains the tuple listed above, where <Team> is a string data type, <Point> an integer data type, and <Rank> is an integer datatype. For example:
received rankTable: with ties, odered by points
[ ['Brazil', 5, 1], ['Argentina', 5, 1], ['Italy', 4, 3], ['Spain', 2, 4] ]
This table, as you can appreciate, is sorted by points (again see Task 5 for better understanding). Now we need to break the ties among teams of equal rank. The resulting rankTable without ties, which is returned, should be:
rankTable to return: without ties, ordered by points and names
[['Argentina', 5, 1], ['Brazil', 5, 2], ['Italy', 4, 3], ['Spain', 2, 4] ]
In order to complete this task, you may follow the algoithm specified below (this is optional, you can come up with your own algorithm). For a better understanding we will start our explanation with the rankTable received as parameter specified above.
1. Teams with equal ranks must be sorted by names (i.e., alphabetically ascending order). For this, use the function sortTable to sort the rankTable with respect to column 0 (i.e., team names) indicating the first (startRow) and last (endRow) rows with equal ranks. Specifically you need to iterate each row of the rankTable:
- Find the startRow and endRow (both inclusive) among which ranks are equal. That is, the first and last row with equal rank.
- Use sortTable to sort with respect to column <Rank> starting from startRow to endRow in the ascending order.
In our example the startRow is 0 and the endRow row would be 1. The resulting sorted rankTabe would be:
[['Argentina', 5, 1], ['Brazil', 5, 1], ['Italy', 4, 3], ['Spain', 2, 4] ]
2. You need make sure that all teams involved in a tie are ordered by names. At that point you will have rankTable sorted with respect to both points (received as parameter already sorted by points) and names. Then, you may update the rank of each team with the corresponding row-index of the rankTable. How? Just assigning/updating the rank values from 1 to 4 starting in row 0.
In our example after following this procedure, the resulting rankTable would be:
[['Argentina', 5, 1], ['Brazil', 5, 2], ['Italy', 4, 3], ['Spain', 2, 4] ]
3. Return the rankTable (witout ties and sorted by points and names) obtained in point 2.
Unit Test from file testCases.py: testTask4() is a unit test for function breakTies.
def testTask4():
rankTable = [['Bangladesh', 6, 1], \
['Brazil', 6, 1], \
['Panama', 6, 1], \
['Argentina', 0, 4]]
rankTable = breakTies(rankTable)
for r in rankTable:
print (r)Sample Output: Expected output of testTask4() is following:
['Bangladesh', 6, 1] ['Brazil', 6, 2] ['Panama', 6, 3] ['Argentina', 0, 4]
Task 5: Write the function that adds ranks to the Points Table
In this task, you need to write the function addRankToPointsTable(pointsTable).
The function addRankToPointsTable(pointsTable) adds a column to the pointsTable to indicate the rank (from 1 to 4) of each team of a particular group. Each team will be ranked differently form 1 to 4 (there will not be two teams with the same rank).
This function is called from buildRankTable function for a specific group. It receives pointsTable as parameter of the following form:
| <Team> | <Point> |
The parameter pointsTable is a list of lists. Every inner list contains the tuple listed above, where <Team> is of string datatype, and <Point> is of integer datatype. For example:
pointsTable:
[ ['Italy', 4], ['Brazil', 5], ['Argentina', 5], ['Spain', 2]]
You just need to add rank information. The format of the resulting rankTable will be as follows:
| <Team> | <Point> | <Rank> |
This table is represented as a list of lists. Every inner list contains the tuple listed above, where <Team>is a string data type, <Point> an integer data type, and <Rank> is an integer datatype. For example:
rankTable:
[ ['Argentina', 5, 1], ['Brazil', 5, 2], ['Italy', 4, 3], ['Spain', 2, 4]]
As you can see, in the resulting rankTable each <Team> is ranked between 1 and 4 according to its<Point>. The <Team> with most point is ranked 1, the second team with the second maximum points is ranked 2 and so on. If two or more teams have equal points, as Brazil and Argentina, you will have to use the method later in this section to break the ties.
In order to complete this task, you can follow the algorithm provided below (this optional, you can come up with your own algorithm). For a better understanding we will start our explanation with the pointsTable received as parameter specified above.
1. Sort the poinsTable (descending order, i.e., largest to smallest) according to the points. You can do this using the sortTable function. The resulting sorted pointsTable would be:
[ ['Brazil', 5], ['Argentina', 5], ['Italy', 4], ['Spain', 2]]
2. Initialize rankTable with empty list, i.e., rankTable = [].
3. Copy the sorted pointsTable to the list rankTable. In this case rankTable would be:
[ ['Brazil', 5], ['Argentina', 5], ['Italy', 4], ['Spain', 2]]
4. Add ranks (an extra column) to the rankTable. Initially assign a rank of 0 to all the teams. The resulting rankTable would be:
[ ['Brazil', 5, 0], ['Argentina', 5, 0], ['Italy', 4, 0], ['Spain', 2, 0]]
5. Update ranks. Since rankTable is sorted with respect to points, you can take advantage of this fact during the updating process. Rank 1 to the team at row 0 of rankTable. Teams at subsequent rows in the order will get a rank value that is equal to the rank of the previous row + 1. In case that points in a row are equal to points in the previous row the rank assigned to that particular row is the same rank as the previous one. For example:
[ ['Brazil', 5, 1], ['Argentina', 5, 1], ['Italy', 4, 3], ['Spain', 2, 4]]
6. Now we need to break ties. In order to break ties you will call the function breakTies. The functionbreakTies receives a rankTable (like the one showed above) as paramenter, checks for ties, fix the ties and returns the rankTable like following:
[ ['Argentina', 5, 1], ['Brazil', 5, 2], ['Italy', 4, 3], ['Spain', 2, 4]]
Notice that at the end of The function addRankToPointsTable you return the table returned bybreakTies. In this case:
[ ['Argentina', 5, 1], ['Brazil', 5, 2], ['Italy', 4, 3], ['Spain', 2, 4]]
Unit Test from file testCases.py: testTask5() is a unit test for function addRankToPointsTable.
def testTask5():
pointsTable = [['Colombia', 3], \
['Japan', 4], \
['Mexico', 3], \
['France', 7]]
rankTable = addRankToPointsTable(pointsTable) # note that addRankToPointsTable calls breakTies function
for r in rankTable:
print (r)Sample Output: Expected output of testTask5() is following:
>>> ================================ RESTART ================================ ['France', 7, 1] ['Japan', 4, 2] ['Colombia', 3, 3] ['Mexico', 3, 4] >>>
Task 6: Write the function that builds the Rank Table
In this task, you need to implement the function buildRankTable(groupName) for round 0.
The function buildRankTable(groupName) receives groupName (data type string) as a parameter and returns a rank table. Four possible values of groupName are: 'groupA', 'groupB', 'groupC', 'groupD'. Again, you must not use other values for goupName.
To complete this function you need to do the following four tasks:
- Read groupInfo, i.e., the teams belong to groupName.
- Get the results for the teams of groupName.
- Compute the points table for groupName.
- Add a column called rank to the points table, and get the rankTable.
Finally, you need to return the rankTable.
You need the following two input files to build the rank table:
groupInfo.txtfile contains the 'group information for all the teamsround0Results.txtfile contains results of each match in round 0 stage.
The format of the rank table for a group is the following:
| <Team> | <Point> | <Rank> |
The table is represented as a list of lists. Every inner list contains a tuple listed above, where <Team> is a string data type, <Point> is an integer data type and <Rank> is an integer data type between 1 and 4. Example:
The list of lists of datatypes are following: [ [str, int, int], [str, int, int] ]. An example of such rank table is following:
rankTable:
[ ['Argentina', 5, 1], ['Brazil', 5, 2], ['Germany', 4, 3], ['USA', 2, 4] ]
Unit Test from file testCases.py: testTask6() is a unit test for function buildRankTable.
def testTask6():
group = 'groupA'
rankTableA = buildRankTable(group)
print(rankTableA)Sample Output Expected output of testTask6() is following:
>>> ================================ RESTART ================================ >>> [['France', 7, 1], ['Japan', 4, 2], ['Colombia', 3, 3], ['Mexico', 3, 4]] >>>
Task 7: Write a function that builds a Crossed Table
In this task, you need to implement the function buildCrossedTable(rankTableGroupX, rankTableGroupY).
The function buildCrossedTable(rankTableGroupX, rankTableGroupY) creates a cross table from the rank table of two different groups. It simply takes the winner of group X and matches it with the runner-up of group Y. Similarly, it takes the winner of group Y and matches with the runner-up of the group X.
The format of the resulting match table is as follows:
| <Winner> | <Runner-up> |
This table is represented as a list of lists. Every inner list contains the pair listed above, where <Winner> and <Runner-up> are of string data type. Specifically this table will be written using a list as follows:
[[<winnerX>, <runner_upY>], [[<winnerY, runner_upX>]]
Parameter: rankTableGroupX
- List of the format
[[str, int, int],..., [str, int, int]]
- For example:
[['Japan', 6, 1], ['Colombia', 4, 2], ['Mexico', 4, 3], ['France', 3, 4]]
Parameter: rankTableGroupY
- List of the format
[[str, int, int],..., [str, int, int]]
- For example:
[['USA', 9, 1], ['Saudi Arabia', 4, 2], ['India', 3, 3], ['Argentina', 1, 4]]
Returns: cross table
- List of the format
[[str, str], [str, str]]
- For example:
[['Japan', 'Saudi Arabia'], ['USA', 'Colombia']]
Unit Test from file testCases.py: testTask7() is a unit test for function buildCrossTable.
def testTask7():
rankTableA = [['Japan', 6, 1], ['Colombia', 4, 2],\
['Mexico', 4, 3], ['France', 3, 4]]
rankTableB = [['USA', 9, 1], ['Saudi Arabia', 4, 2], \
['India', 3, 3], ['Argentina', 1, 4]]
crossedTableAB = buildCrossedTable(rankTableA, rankTableB)
print(crossedTableAB)Sample Output Expected output of testTask7() is following:
>>> ================================ RESTART ================================ >>> [['Japan', 'Saudi Arabia'], ['USA', 'Colombia']] >>>
Task 8: Write a function that determines the winner of a game
In this task you need to implement the function determineWinner.
The function determineWinner determines the winner of a match given a list of strings if two teams. The format of the list is as follows:
[<Team1>, <Team2>]
Where <Team1> and <Team2> are strings.
The function makes use of the file breaker.txt to determine the winner. The function returns the winner, which is string data type.
Parameter: teams
- List of format [str, str],
Returns: winner team
- Data type: string
Unit Test from file testCases.py: testTask8() is a unite test for function determineWinner.
def testTask8():
teams = ['Panama', 'India']
winner = determineWinner(teams)
print(winner)Sample Output Expected output of testTask8() is following:
>>> ================================ RESTART ================================ >>> Panama >>>
Task 9: Plot statistics of three best teams of the world cup
In this function you need to implement the function plotGoalsBarChart.
The function plotGoalsBarChar plots a bar chart of the number of goals for and against the champion, sub-champion and third-place winning teams of the World Cup in round 0. We want to see if the results of round 0 reflect the final result of the World Cup.
The function receives a list of strings that contains the best three teams in the following order:
[<champion>, <sub-champion>, <third-place>]
Where <champion>, <sub-champion>, and <third-place> are string data type.
You must use the file file round0Results.txt to count the number of goals for and against of each of the three teams. The bar chart must look similar to the one presented previously in the Project Description section.
Please notice the y-axis scale. It must be set to 35 since in round 0 the maximum score of any team is 10 and each team plays 3 games.
Unit Test from file testCases.py: testTask9() is a unit test for function plotGoalsBarChar.
def testTask9():
finalResults = ['Brazil', 'Germany', 'Italy']
plotGoalsBarChar(finalResults)Sample Output Expected output of testTask9() is following:
10 years ago
50
- PARTON WHOLESALE COMPANY 1616
- How put a trash and where its go?
- Research-Based Interventions on Stress Disorders, Anxiety Disorders, Conduct Disorders, and ADHD Paper Select a stress disorder, anxiety disorder, conduct disorder, or ADHD...
- accounting ii
- Experimental and Non-experimental design
- UNIT II MINI PROJECT IS CHAPTERS 4 AND 5 1436
- Information System part II
- FROM THE E-ACTIVITY, RECOMMEND AT LEAST THREE BEST PRACTICES 1381
- COST FUNCTION 1370
- Saint GBA334 Final exam 2015 (New) All correct